package com.schd.base.enumerate;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;

/**
 *     数据类型
*/
public enum DataType {
    String, Integer, Long, Float, Double, BigDecimal, BigInteger, Boolean, Date, Timestamp;
    public static Object[] convert(String[] strArray, DataType type,
                                   SearchOperator operator) {
        if (ArrayUtils.isEmpty(strArray)) {
            return null;
        }
        List<Object> list = new ArrayList<Object>();
        for (java.lang.String str : strArray) {
            if (StringUtils.isBlank(str)) {
                continue;
            }
            switch (type) {
                case Integer:
                    list.add(NumberUtils.createInteger(str));
                    break;
                case Long:
                    list.add(NumberUtils.createLong(str));
                    break;
                case Float:
                    list.add(NumberUtils.createDouble(str));
                    break;
                case Double:
                    list.add(NumberUtils.createDouble(str));
                    break;
                case BigDecimal:
                    list.add(NumberUtils.createBigDecimal(str));
                    break;
                case BigInteger:
                    list.add(NumberUtils.createBigInteger(str));
                    break;
                case Boolean:
                    list.add(java.lang.Boolean.valueOf(str));
                    break;
                case Date:
                    DateTime dt = DateTime.parse(str);
                    if (str.length() <= 10 && operator == SearchOperator.LTE) {
                        dt = dt.plusDays(1);
                        dt = dt.minusMillis(1);
                    }
                    list.add(dt.toDate());
                    break;
                case Timestamp:
                    list.add(new java.sql.Timestamp(DateTime.parse(str).getMillis()));
                    break;
                default:
                    list.add(str);
            }
        }
        return list.toArray();
    }

}
